package com.bonc.rms.controller;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.bonc.rms.entity.UserEntity;
import com.bonc.rms.service.UserService;
/**
 * 用户管理控制器
 * @author zlq
 * @time 2017年1月12日下午
 */

@Controller
@RequestMapping(value = "/userManage")
public class UserManageController {

	@Autowired
	private UserService userService;

	/**
	 * 查找所有用户
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/findAllUsers",method = RequestMethod.GET)
	public Map<String, Object> findAllUsers(){											
		Map<String, Object> usersMap = new HashMap<String, Object>();
		List<UserEntity> userList = new LinkedList<>();	
		userList = userService.findAllUsers();
		usersMap.put("userList", userList);
		return usersMap;
	}

	/**
	 * 根据条件查找用户
	 * @param user_name
	 * @param department
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/findUsers")
	public String findUsers(@RequestParam(value = "managerUser_name",defaultValue = "") String user_name,
							@RequestParam(value = "selectGroup",defaultValue = "") String department){
		Map<String, Object> usersMap = new HashMap<String, Object>();
		List<UserEntity> userList = new LinkedList<>();
		userList = userService.findByUserNameAndDepartment(user_name, department);
		usersMap.put("userList", userList);
		return JSON.toJSONString(usersMap);
	}
	
	
	/**
	 * 根据id查询用户
	 * @param id
	 */

	@ResponseBody
	@RequestMapping(value = "/findUserById")
	public Map<String, Object> findUserById(@RequestParam("id") Integer id){
		
		Map<String, Object> userMap = new HashMap<String, Object>();
		UserEntity user = new UserEntity();
		user = userService.findUserById(id);
		userMap.put("user", user);
		return userMap;
	}

	
	
	/**
	 * 根据 id 更新用户
	 * @param id
	 * @param user_name
	 * @param passwd
	 * @param department
	 * @param authority
	 */
 
	@RequestMapping(value = "/updateUserById")
	public String updateUser(@RequestParam("id") Integer id,
							 @RequestParam("user_name") String user_name,
							 @RequestParam("passwd") String passwd,
							 @RequestParam("state") String state,
							 @RequestParam("department") String department,
							 @RequestParam("authority") String authority){
		UserEntity user = new UserEntity();
		user.setId(id);
		user.setUserName(user_name);
		user.setPasswd(passwd);
		user.setState(state);
		user.setDepartment(department);
		user.setAuthority(authority);
		userService.saveUser(user);
		return "user/managerPage";
	}

	
	/**
	 * 根据id删除用户
	 * @param id
	 * @return 
	 */

	@RequestMapping(value = "/deleteUser")
	public String deleteUser(@RequestParam("id") Integer id){
		
		userService.deleteUser(id);
		return "user/managerPage";
	}

	/**
	 * 新增用户
	 * @param user_name
	 * @param passwd
	 * @param department
	 * @param authority
	 */

	@RequestMapping(value = "/addUser")
	public String addUser(@RequestParam("user_name") String user_name,
						  @RequestParam("passwd") String passwd,
						  @RequestParam("department") String department,
						  @RequestParam(value = "state",defaultValue = "正常") String state,
						  @RequestParam("authority") String authority
							 ){
		
		UserEntity user = new UserEntity();
		user.setUserName(user_name);
		user.setPasswd(passwd);
		user.setDepartment(department);
		user.setAuthority(authority);
		user.setState(state);
		userService.saveUser(user);
		return "user/managerPage";
	}
	
	/**
	 * 状态切换
	 * @param id
	 * @param state
	 * @return
	 */

	@RequestMapping(value = "/toggleState")
	public String updateStateById(@RequestParam("id") Integer id,
								  @RequestParam("state") String state){
		
		userService.updateStateById(state, id);
		return "user/managerPage";	
		}

	/**15632590341
	 * 6613960
	 * @param httpSession
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/showInfo")
	public String showUserInfo(HttpSession httpSession){
		Map<String, Object> userMap = new HashMap<String,Object>();
		UserEntity user = new UserEntity();
		String userName = (String) httpSession.getAttribute("name");
		user = userService.findByUserName(userName);
		userMap.put("user", user);
		return JSON.toJSONString(userMap);
	}
	
	/**
	 * 
	* @Title: showUserInfo
	* @Description: TODO
	* @param @param httpSession
	* @param @return  
	* @return String 
	* @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/initStudentMessage")
	public String initStudentMessage(HttpSession httpSession){
		Map<String, Object> userMap = new HashMap<String,Object>();
		UserEntity user = new UserEntity();
		String userName = (String) httpSession.getAttribute("name");
		user = userService.findByUserName(userName);
		userMap.put("user", user);
		return JSON.toJSONString(userMap);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
